﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ZDT.Task.TPurview
{
    /// <summary>
    /// 导航任务
    /// </summary>
    public class PurviewTask
    {
        public PurviewSetting Setting { get; set; }
        public List<PurviewData> Data { get; set; }
        private int index = 0;
        private List<PurviewInfo> infos = null;
        private System.Threading.Thread tash = null;
        public GlobalParas.TaskState State { get; set; }
        protected virtual void Init()
        {
            State = GlobalParas.TaskState.UnStart;
            infos = new List<PurviewInfo>();
            foreach (var item in Data)
            {
                var info = new PurviewInfo();
                info.Id = item.Id;
                info.LoginUrl = string.Format(Setting.LoginUrlFmt, info.Id);
                info.AddUrl = string.Format(Setting.AddUrlFmt, info.Id);
                info.RefreshUrl = string.Format(Setting.RefreshUrlFmt, info.Id);
                info.LastRefresh = DateTime.Now.AddHours(8);
                info.Data = item.Data;
                infos.Add(info);
            }
            tash = new System.Threading.Thread(() =>
            {
                for (int k = index, len = infos.Count; k < len; k++)
                {
                    if (State == GlobalParas.TaskState.Started)
                    {
                        index = k;
                        One(infos[k]);
                        Thread.Sleep((5 + Setting.Wait + Setting.LoginWait + Setting.AddWait + Setting.RefreshWait + Setting.NextWait) * 1000);
                    }
                }
            });
        }
        public void Start()
        {
            if (tash == null)
            {
                Init();
            }
            State = GlobalParas.TaskState.Started;
            tash.Start();
        }
        protected virtual void One(PurviewInfo info)
        {

        }
        public void Pause()
        {
            State = GlobalParas.TaskState.Pause;
            tash.Abort();
        }
        public void Stop()
        {
            State = GlobalParas.TaskState.Stopped;
            tash.Abort();
        }
    }
}
